//
//  YHOAuthViewController.swift
//  SniaWeiBo
//
//  Created by lcc on 16/5/12.
//  Copyright © 2016年 lcc. All rights reserved.
//

import UIKit
//授权码
let weiboAppKey = "3469971745"
//回调页
let weiboRedirect_uri = "http://www.itcast.cn"

class YHOAuthViewController: UIViewController {
    
        //创建webView
    private lazy var webView: UIWebView = UIWebView()
    
    //自定义view
    override func loadView() {
        
        view = webView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupNavUI()
        
        //  请求登陆地址
        let url = "https://api.weibo.com/oauth2/authorize?client_id=\(weiboAppKey)&redirect_uri=\(weiboRedirect_uri)"
        let request = NSURLRequest(URL: NSURL(string: url)!)
        
        print(url)
        //加载登录界面
        webView.loadRequest(request)
    }
    
    private func setupNavUI() {
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", fontSize: 15, target: self, action: "cancelAction")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", fontSize: 15, target: self, action: "autoFillAction")
        navigationItem.title = "微博登陆"
    }
    
    //  点击事件
    @objc private func cancelAction() {
        print("cancelAction")
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    @objc private func autoFillAction() {
        print("autoFillAction")
        webView.stringByEvaluatingJavaScriptFromString("document.getElementById('userId').value = 'hao123guohaibin@163.com'; document.getElementById('passwd').value = 'guohaibin012'")
    }
}
